Beginning Kotlin by 2023

Beginning Kotlin by 2023

Author:2023
Language: eng
Format: epub


ChapteR 4 tYpes

➊ seeing the final and override keywords on the same line seems odd, but it’s perfectly legal. It means we are overriding the function and, at the same time, closing it for further inheritance. the final keyword in this function affects only subtypes of the employee class, but not the employee class itself.

➋ this statement won’t compile anymore. the talk() function was marked final in the supertype (employee class).

Properties

Properties are important for POJOs (Plain OldJava Objects). They’re useful for modeling domain objects. Typically (in Java), you create properties by defining member variables—usually by following naming conventions—

and these variables are customarily prefixed by get and set. After that, you’ll create accessor methods to get and set the values of the member variables.

Listing 4-18 shows a typical POJO in Java with getter and setter methods.

Listing 4-18. Person class in Java with a single property public class Person {

private String name;

public String getName() {

return this.name;

}

public void setName(String name) {

this.name = name

}

93

ChapteR 4 tYpes

public static void main(String []args) {

Person person = new Person();

person.setName("John Doe");

System.out.println(person.getName());

}

}

In the preceding example, we have class Person with just one property ( name). We did this by making the name variable private so we can control the variable’s state only via accessor methods. We had to do this kind of coding in Java because the language did not have native support for properties. We don’t have to do this in Kotlin because it has language support for properties. Let’s rewrite an equivalent of the previous POJO

example in Kotlin (see Listing 4-19).

Listing 4-19. Person class with a single property

class Person(_name:String) { ➊

val name:String = _name ➋

}

fun main(args: Array<String>) {

var person = Person("John Smith")

println(person.name) ➌

}

➊ a constructor takes in a parameter; this lets us set the name of the object at the time of object creation.

➋ We can access constructor parameters in the class body.

➌ this statement may look like we are directly accessing the name member variable, but we are not. this statement actually calls the get accessor method.

Kotlin works in the background to provide automatic accessors and backing fields.

94

ChapteR 4 tYpes

We can further simplify the Person example; Listing 4-20

shows us how.

Listing 4-20. Simplified Person class

class Person(val name:String)

fun main(args: Array<String>) {

var person = Person("John Smith")

println(person.name)

}

The code above is the most concise way of defining a property in

Kotlin. It’s also considered idiomatic. Notice the changes we made in the code.

• The parameter in the primary constructor now has a

val declaration which makes the constructor parameter

an immutable property. If you want to make a mutable

(read/write) property, use the var keyword instead.

• We no longer need to differentiate the identifier in

the constructor parameter with the member variable;

hence we dropped the leading underscore in the

_name variable.

• We can drop the entire class body since we no longer

need it. The class body only contains the code to

transfer the value of the constructor parameter to the

member variable. Since Kotlin will automatically define

a backing field for the constructor parameter, we don’t

have to do anything anymore in the class body.

The code in Listing 4-20 shows the most



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.